package controlService;

import java.util.*;

import models.*;

public final class TcardDB {
	
	public static final TcardDB DB = new TcardDB();
	
	private Map<UUID, Tcard> _tcards;

	// This is a Singleton class!  Access it statically via TcardDB.DB
	private TcardDB()
	{
		_tcards = new HashMap<UUID, Tcard>();
	}	
	
	public Collection<Tcard> getAll()
	{
		return _tcards.values();		
	}
	
	public Tcard getById(UUID id)
	{
		return _tcards.get(id);
	}
	
	public Collection<Tcard> getSince(Date time)
	{	
		Collection<Tcard> tcardsSince = new ArrayList<Tcard>();
		
		for(Tcard tcard : _tcards.values())
			if(tcard.getModified().after(time))
				tcardsSince.add(tcard);
		
		return tcardsSince;
	}
	
	public synchronized void save(Tcard tcard)
	{
		_tcards.put(tcard.getId(), tcard);
	}
	
	public synchronized void delete(Tcard tcard)
	{
		_tcards.remove(tcard.getId());
	}
}
